package ui 
{
	import Base.Loader.SWFLoader;
	
	import Common.GameDefine;
	import Common.GameNetManager;
	import Common.RenderManager;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	
	import logic.GameItem;
	
	import net.ItemNetHandler;
	

	/**
	 * ...
	 * @author ...
	 */
	public class ItemUIHandler extends uiBase
	{
		private var mouseLoader:Loader = null;
		private var dragItem:GameItem = null;
		
		private var selectItem:GameItem = null;
		

		public function get SelectItem():GameItem
		{
			return selectItem;
		}
		
		public function clearSelectItem():void
		{
			if ( selectItem )
			{
				uiMc.select( selectItem.i , false );
				selectItem = null;
			}
		}
		
		public function clearItem( i:int ):void
		{
			uiMc.clearItem( i );
			selectItem = null;
		}
		
		public function updateSelectItem():void
		{
			if ( selectItem )
			{
				uiMc.updateItem( selectItem.i , selectItem.id , selectItem.num , selectItem.image , selectItem.px , selectItem.py );
			}
		}

		
		public function ItemUIHandler() 
		{
			path = "item.swf";
			name = "ItemUI";
		}
		
		
		public override function onInit( loader:SWFLoader ):void
		{
			super.onInit( loader );
			
			mouseLoader = new Loader();
			mouseLoader.mouseChildren = false;
			mouseLoader.mouseEnabled = false;
			
			uiMc.addChild( mouseLoader );
			uiMc.closeButton.addEventListener( MouseEvent.CLICK , onClose );
			
			for ( var i:int = 0 ; i < uiMc.itemArray.length ; i++ )
			{
				uiMc.itemArray[ i ].addEventListener( MouseEvent.MOUSE_DOWN , onMouseDown( i ) );
				uiMc.itemArray[ i ].addEventListener( MouseEvent.MOUSE_UP , onMouseUp( i ) );
				
				uiMc.itemArray[ i ].addEventListener( MouseEvent.MOUSE_OVER , onMouseOver( i ) );
				uiMc.itemArray[ i ].addEventListener( MouseEvent.MOUSE_OUT , onMouseOut );
				
				uiMc.itemArray[ i ].addEventListener( MouseEvent.DOUBLE_CLICK , onMouseDoubleClick( i ) );
			}
			
			for ( i = 0 ; i < uiMc.equipArray.length ; i++ )
			{
				uiMc.equipArray[ i ].addEventListener( MouseEvent.MOUSE_DOWN , onMouseDown1( i ) );
				uiMc.equipArray[ i ].addEventListener( MouseEvent.MOUSE_UP , onMouseUp1( i ) );
				
				uiMc.equipArray[ i ].addEventListener( MouseEvent.MOUSE_OVER , onMouseOver1( i ) );
				uiMc.equipArray[ i ].addEventListener( MouseEvent.MOUSE_OUT , onMouseOut );
				
				//uiMc.equipArray[ i ].addEventListener( MouseEvent.DOUBLE_CLICK , onMouseDoubleClick( i ) );
			}
			
			RenderManager.instance.stage.addEventListener( MouseEvent.MOUSE_MOVE , onMouseMove );
			RenderManager.instance.stage.addEventListener( MouseEvent.MOUSE_UP , onMouseUp2 );
			
			updateData();
		}
		
		public override function resize():void
		{
			if ( uiMc )
			{
				uiMc.x = RenderManager.instance.stageWidth - uiMc.px - 15;
				uiMc.y = RenderManager.instance.stageHeight * 0.1 + uiMc.py;
			}
		}
		
		public function onMouseMove( e:MouseEvent ):void
		{
			if ( dragItem )
			{
				mouseLoader.x = uiMc.mouseX + dragItem.px;
				mouseLoader.y = uiMc.mouseY + dragItem.py;
			}
		}
		
		public function updateData():void
		{
			if ( !uiMc )
			{
				return;
			}
			
			uiMc.clear();
			
			var itemArray:Array = GameManager.instance.MainPlayer.itemArray;
			
			for ( var i:int = 0 ; i < itemArray.length ; i++ )
			{
				var item:GameItem = itemArray[ i ];
				
				if ( item )
				{
					uiMc.setItem( i , item.id , item.num , item.image , item.px , item.py );
				}
			}
			
			var equipArray:Array = GameManager.instance.MainPlayer.equipArray;
			
			for ( i = 0 ; i < equipArray.length ; i++ )
			{
				var item1:GameItem = equipArray[ i ];
				
				if ( item1 )
				{
					uiMc.setEquip( i , item1.id , item1.num , item1.image , item1.px , item1.py );
				}
			}
			
			uiMc.setStone( GameManager.instance.MainPlayer.stone );
		}
		
		
		public function onMouseUp2( e:MouseEvent ):void
		{
			if ( selectItem )
			{
				return;
			}
			
			if ( dragItem )
			{
				if ( mouseLoader.x > -uiMc.width * 0.5 && mouseLoader.y > -uiMc.height * 0.5 && mouseLoader.x < uiMc.width * 0.5 && mouseLoader.y < uiMc.height * 0.5 )
				{
					
				}
				else
				{
					mouseLoader.unload();
					dragItem = null;
				}
			}
		}
		
		
		public function onMouseUp( i:int ):Function
		{
			return function foo( e:MouseEvent ):void
			{
				if ( selectItem )
				{
					return;
				}
				
				if ( dragItem )
				{
					var mainPlayer:GamePlayer = GameManager.instance.MainPlayer;
					
					if ( dragItem.item )
					{
						// item to item
						
						if ( dragItem.i != i )
						{
							var dragI1:int = dragItem.i;
							
							uiMc.switchItem( dragI1 , i );
							
							mainPlayer.switchItem( dragI1 , i );
							ItemNetHandler.sendSwitchItem( dragI1 , i );
							
							uiMc.setExplain( dragItem.name , "" , dragItem.explain );
						}
						
					}
					else
					{
						// equip to item
						
						var item1:GameItem = mainPlayer.getItem( i );
						var dragI:int = dragItem.i;
						
						if ( item1 )
						{
							var equip:int = mainPlayer.canEquipItem( i , dragI );
							
							if ( equip == dragItem.i )
							{
								mainPlayer.useItem( i , dragI );
								uiMc.switchItemEquip( i , dragI );
								
								dragItem = mainPlayer.getItem( i );
								uiMc.setExplain( dragItem.name , "" , dragItem.explain );
								
								ItemNetHandler.sendUseItem( i , dragI , GameItem.UIT_PLAYER );
							}
						}
						else
						{
							mainPlayer.dropEquip( dragI , i );
							uiMc.switchItemEquip( i , dragI );
							
							ItemNetHandler.sendDropItem( dragI , i );
						}
						
						
					}
					
					mouseLoader.unload();
					dragItem = null;				
				}
			}
		}
		
		
		public function onMouseDown( i:int ):Function
		{
			return function foo( e:MouseEvent ):void
			{
				if ( selectItem )
				{
					return;
				}
				
				var mainPlayer:GamePlayer = GameManager.instance.MainPlayer;
				var item:GameItem = mainPlayer.getItem( i );
				
				if ( item )
				{
					dragItem = item;
					
					mouseLoader.load( new URLRequest( GameDefine.PATH + GameDefine.PATH1 + "textureItem/" + dragItem.image.toString() + ".png" ) );
					mouseLoader.x = uiMc.mouseX + dragItem.px;
					mouseLoader.y = uiMc.mouseY + dragItem.py;
				}
			}
		}
		
		
		public function onMouseUp1( i:int ):Function
		{
			return function foo( e:MouseEvent ):void
			{
				if ( selectItem )
				{
					return;
				}
				
				if ( dragItem )
				{
					var mainPlayer:GamePlayer = GameManager.instance.MainPlayer;
					
					if ( dragItem.item )
					{
						// item to equip
						var equip:int = mainPlayer.canEquipItem( dragItem.i , i );
						
						if ( equip == i )
						{
							var dragI:int = dragItem.i;
							
							mainPlayer.useItem( dragI , i );
							uiMc.switchItemEquip( dragI , i );
							
							dragItem = mainPlayer.getEquip( i );
							uiMc.setExplain( dragItem.name , "" , dragItem.explain );
							
							ItemNetHandler.sendUseItem( dragI , i , GameItem.UIT_PLAYER );
						}
					}
					else
					{
						// equip to equip
						
						if ( dragItem.i != i && dragItem.i == GameItem.ET_RING1 || dragItem.i == GameItem.ET_RING2 )
						{
							if ( i == GameItem.ET_RING1 || i == GameItem.ET_RING2 )
							{
								var dragI1:int = dragItem.i;
								
								mainPlayer.switchEquip( dragI1 , i );
								uiMc.switchEquip( dragI1 , i );
								
								dragItem = mainPlayer.getEquip( i );
								uiMc.setExplain( dragItem.name , "" , dragItem.explain );
								
								ItemNetHandler.sendSwitchEquip( dragI1 , i );
							}
						}
					}
					
					mouseLoader.unload();
					dragItem = null;				
				}
			}
		}
		
		
		public function onMouseDown1( i:int ):Function
		{
			return function foo( e:MouseEvent ):void
			{
				if ( selectItem )
				{
					return;
				}
				
				var mainPlayer:GamePlayer = GameManager.instance.MainPlayer;
				var item:GameItem = mainPlayer.getEquip( i );
				
				if ( item )
				{
					dragItem = item;
					
					mouseLoader.load( new URLRequest( GameDefine.PATH + GameDefine.PATH1 + "textureItem/" + dragItem.image.toString() + ".png" ) );
					mouseLoader.x = uiMc.mouseX + dragItem.px;
					mouseLoader.y = uiMc.mouseY + dragItem.py;
				}
			}
		}
		
		
		public function onMouseDoubleClick( i:int ):Function
		{
			return function foo( e:MouseEvent ):void
			{
				if ( selectItem )
				{
					return;
				}
				
				var mainPlayer:GamePlayer = GameManager.instance.MainPlayer;
				var item:GameItem = mainPlayer.getItem( i );
				
				if ( item )
				{
					// use item
					var p:int = mainPlayer.useItem( i , GameDefine.INVALIDID );
					
					if ( p != GameItem.ET_COUNT )
					{
						uiMc.switchItemEquip( i , p );
						
						item = mainPlayer.getItem( i );
						
						if ( item )
						{
							uiMc.setExplain( item.name , "" , item.explain );
						}
						else
						{
							uiMc.clearExplain();
						}
						
						ItemNetHandler.sendUseItem( i , p , GameItem.UIT_PLAYER );
					}
					else
					{
						selectItem = item;
						uiMc.select( i , true );
						TeamUIHandler.instance.visible( true );
					}
					
					
				}
			}
		}
		
		
		public function onMouseOver( i:int ):Function
		{
			return function foo( e:MouseEvent ):void
			{
				var mainPlayer:GamePlayer = GameManager.instance.MainPlayer;
				var item:GameItem = mainPlayer.getItem( i );
				
				if ( item )
				{
					uiMc.setExplain( item.name , "" , item.explain );
				}
			}
		}
		
		public function onMouseOver1( i:int ):Function
		{
			return function foo( e:MouseEvent ):void
			{
				var mainPlayer:GamePlayer = GameManager.instance.MainPlayer;
				var item:GameItem = mainPlayer.getEquip( i );
				
				if ( item )
				{
					uiMc.setExplain( item.name , "" , item.explain );
				}
			}
		}
		
		
		public function onMouseOut( e:MouseEvent ):void
		{
			uiMc.clearExplain();
		}
		
		public function onClose( e:MouseEvent ):void
		{
			visible( false );
			
			clearSelectItem();
			TeamUIHandler.instance.visible( false );
		}
		
		
		public static var instance:ItemUIHandler = null;
		public static function Instance():ItemUIHandler
		{
			if ( instance == null )
			{
				instance = new ItemUIHandler();
			}
			
			return instance;
		}
		
		
	}
	

}
